From d4befbb5c9a924b84a241d2f089e0805d3f4a731 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 Jul 2017 00:30:38 +0300 Subject: [PATCH] Extract clean_benches function --- src/cargo/util/toml/targets.rs | 86 +++++++++++++++++----------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 4b7f81cb6..5cd8ece1a 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -50,26 +50,15 @@ pub fn targets(manifest: &TomlManifest, clean_tests(manifest.test.as_ref(), package_root, &layout)? ); - let benches = match manifest.bench { - Some(ref benches) => { - for target in benches { - target.validate_bench_name()?; - } - benches.clone() - } - None => inferred_bench_targets(&layout) - }; - - if let Err(e) = unique_names_in_targets(&benches) { - bail!("found duplicate bench name {}, but all binary targets \ - must have a unique name", e); - } + targets.extend( + clean_benches(manifest.bench.as_ref(), package_root, &layout)? + ); // processing the custom build script let new_build = manifest.maybe_custom_build(custom_build, package_root); // Get targets - targets.extend(normalize(package_root, new_build, &benches)); + targets.extend(normalize(package_root, new_build)); Ok(targets) } @@ -208,12 +197,11 @@ impl TomlTarget { match self.name { Some(ref name) => { if name.trim().is_empty() { - Err("test target names cannot be empty".into()) - } else { - Ok(()) + bail!("test target names cannot be empty") } + Ok(()) } - None => Err("test target test.name is required".into()) + None => bail!("test target test.name is required") } } @@ -221,12 +209,11 @@ impl TomlTarget { match self.name { Some(ref name) => { if name.trim().is_empty() { - Err("bench target names cannot be empty".into()) - } else { - Ok(()) + bail!("bench target names cannot be empty") } + Ok(()) } - None => Err("bench target bench.name is required".into()) + None => bail!("bench target bench.name is required") } } @@ -405,6 +392,37 @@ fn clean_tests(toml_tests: Option<&Vec>, Ok(result) } +fn clean_benches(toml_benches: Option<&Vec>, + package_root: &Path, + layout: &Layout) -> CargoResult> { + let benches = match toml_benches { + Some(benches) => benches.clone(), + None => inferred_bench_targets(&layout) + }; + + for target in benches.iter() { + target.validate_bench_name()?; + } + + if let Err(e) = unique_names_in_targets(&benches) { + bail!("found duplicate bench name {}, but all binary targets \ + must have a unique name", e); + } + + let mut result = Vec::new(); + for bench in benches.iter() { + let path = bench.path.clone().unwrap_or_else(|| { + PathValue(Path::new("benches").join(&format!("{}.rs", bench.name()))) + }); + + let mut target = Target::bench_target(&bench.name(), package_root.join(&path.0), + bench.required_features.clone()); + configure(bench, &mut target); + result.push(target); + } + Ok(result) +} + fn configure(toml: &TomlTarget, target: &mut Target) { let t2 = target.clone(); target.set_tested(toml.test.unwrap_or(t2.tested())) @@ -420,8 +438,7 @@ fn configure(toml: &TomlTarget, target: &mut Target) { } fn normalize(package_root: &Path, - custom_build: Option, - benches: &[TomlBenchTarget]) -> Vec { + custom_build: Option) -> Vec { let custom_build_target = |dst: &mut Vec, cmd: &Path| { let name = format!("build-script-{}", cmd.file_stem().and_then(|s| s.to_str()).unwrap_or("")); @@ -429,32 +446,13 @@ fn normalize(package_root: &Path, dst.push(Target::custom_build_target(&name, package_root.join(cmd))); }; - let bench_targets = |dst: &mut Vec, - benches: &[TomlBenchTarget], - default: &mut FnMut(&TomlBenchTarget) -> PathBuf| { - for bench in benches.iter() { - let path = bench.path.clone().unwrap_or_else(|| { - PathValue(default(bench)) - }); - - let mut target = Target::bench_target(&bench.name(), package_root.join(&path.0), - bench.required_features.clone()); - configure(bench, &mut target); - dst.push(target); - } - }; let mut ret = Vec::new(); - if let Some(custom_build) = custom_build { custom_build_target(&mut ret, &custom_build); } - bench_targets(&mut ret, benches, &mut |bench| { - Path::new("benches").join(&format!("{}.rs", bench.name())) - }); - ret } -- 2.30.2